Skip to content

feat: support truncating digests #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 5, 2025
Merged

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Jul 22, 2025

Allow truncating message digests (see 5.1 of Recommendation for Applications Using Approved Hash Algorithms) within sensible limits (as defined by https://github.com/ipfs/boxo/blob/main/verifcid/cid.go#L17-L20).

Closes #328

Allow truncating message digests (see 5.1 of [Recommendation for Applications
Using Approved Hash Algorithms](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-107r1.pdf)) within sensible limits.

Closes #328
@achingbrain achingbrain changed the title feat: support truncated hashes feat: support truncating digests Jul 22, 2025
@achingbrain achingbrain requested review from rvagg and wemeetagain July 22, 2025 09:33
@achingbrain
Copy link
Member Author

Refs ipshipyard/roadmaps#1

Copy link

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we not want to go with a whitelist approach like is done in

I'm not the expert here but from what I understand, allowing a minimum digest length of 20 bytes on sha512 would result in a hash that is not as collision resistant (160 bit length, 80 bits of collision resistance)?

@achingbrain
Copy link
Member Author

do we not want to go with a whitelist approach like is done in

We have something similar to this already in that Helia only ships with support for sha2-256, sha2-512 and identity hashes. It's up to the user to configure other hashes.

I'm not the expert here but from what I understand, allowing a minimum digest length of 20 bytes on sha512 would result in a hash that is not as collision resistant (160 bit length, 80 bits of collision resistance)?

That is correct but if we enforce something different we should change it in Kubo/Boxo too.

return new Hasher(name, code, encode, minDigestLength, maxDigestLength)
}

export interface DigestOptions {
Copy link
Contributor

@2color 2color Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add the unit type in the JS doc so it's clear truncate is in bytes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a doc comment, let me know if you think it's clear.

@rvagg
Copy link
Member

rvagg commented Jul 24, 2025

What's the upstream workflow this solves that this needs to be done internally?
I don't really have a strong opinion here as long as the standard path does the right (complete) thing and we don't end up making truncated CIDs from this without the user going through a hoop or two. It just seems to me that this is basically the same as taking a digest and just truncating it yourself; but with some internal guard rails. Is that right?

@rvagg
Copy link
Member

rvagg commented Jul 24, 2025

To be clear - I'm not blocking this, happy for you to land this, just interested in the workflow and what the upstream demands are that are triggering this.

@achingbrain
Copy link
Member Author

What's the upstream workflow this solves that this needs to be done internally?

People truncate hashes to make them less unwieldy but where the smaller hash still results in some sort of acceptable level of collision protection, at least with sha2-512, maybe others.

Kubo has a whitelist of allowable hashes and a hardcoded limit of 20-128 bytes for hash size, regardless of the hash used so we need some sort of equivalent functionality.

It's probably fair to say that the acceptable lower band for hash length is algorithm-dependant, so this PR is an attempt to allow individual hashes to define what is acceptable and what isn't, rather than having a blanket 20 byte limit.

It just seems to me that this is basically the same as taking a digest and just truncating it yourself; but with some internal guard rails. Is that right?

Pretty much, but with this PR we can offer users a slightly better time in that we can say "this specific algorithm doesn't function under N bytes", without the calling code needing to know which algorithms have which limits - the user can just configured the algorithms they want and get on with things.

*
* @default 20
*/
minDigestLength?: number

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without any other customization will this break small identity multihashes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't do, unless you identity.digest(fromString('test'), { truncate: 3 }), this limit doesn't get touched unless you ask to use the new truncate feature so this change should be entirely backward compatible and non-breaking

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out the types wouldn't allow passing options to identity.digest as it doesn't implement the same interface as things returned from from (see comment) - I've fixed this up here, still non-breaking.

@achingbrain achingbrain merged commit e4d3a22 into master Aug 5, 2025
20 checks passed
@achingbrain achingbrain deleted the feat/support-truncated-hashes branch August 5, 2025 15:21
github-actions bot pushed a commit that referenced this pull request Aug 5, 2025
## [13.4.0](v13.3.7...v13.4.0) (2025-08-05)

### Features

* support truncating digests ([#329](#329)) ([e4d3a22](e4d3a22)), closes [#328](#328)
Copy link

github-actions bot commented Aug 5, 2025

🎉 This PR is included in version 13.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support truncated hashes
5 participants